import pymysql
host='localhost'
port=3306
user='root'
password='1234'
database = 'company'
charset='utf8'
#数据库的增删改操作
def databases_operations(sql,param = None):
    db =pymysql.connect(
        host=host,
        port=port,
        user= user,
        password = password,
        database = database,
        charset = charset
        )
    try:
        with db.cursor() as cursor:
            cursor.execute(sql,param)
            db.commit()
            print('操作成功')
    except Exception as e:
        print('操作有问题',e)
    finally:
        cursor.close()
        db.close()

#查找操作
def select(sql,param = None):
    db =pymysql.connect(
        host=host,
        port=port,
        user= user,
        password = password,
        database = database,
        charset = charset
        )
    try:
        with db.cursor() as cursor:
            cursor.execute(sql,param)
            db.commit()
            print('操作成功')
            data = cursor.fetchall()
            print(data)
            for i in data:
                print(i)
    except Exception as e:
        print('操作有问题',e)
    finally:
        cursor.close()
        db.close()

